﻿using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace DocX_WordDemo
{
    public static class Context
    {
        public static void InsertWatermark(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            Section section = doc.LastSection;
            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.Text = "TextWatermark Demo";
            txtWatermark.FontSize = 90;
            txtWatermark.Layout = WatermarkLayout.Diagonal;
            section.Document.Watermark = txtWatermark;
            doc.SaveToFile(outputFileName);
        }

        public static void InsertSubSuperScript(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            Section section = doc.LastSection;
            Paragraph p = section.AddParagraph();
            p.AppendText("E=mc");
            p.AppendText("2").CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;
            p = section.AddParagraph();
            p.AppendText("F");
            p.AppendText("n").CharacterFormat.SubSuperScript = SubSuperScript.SubScript;
            p.AppendText("=F");
            p.AppendText("n-1").CharacterFormat.SubSuperScript = SubSuperScript.SubScript;
            p.AppendText("+F");
            p.AppendText("n-2").CharacterFormat.SubSuperScript = SubSuperScript.SubScript;
            doc.SaveToFile(outputFileName);
        }

        public static void InsertTextbox(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            Section section = doc.LastSection;
            TextBox tb = section.AddParagraph().AppendTextBox(200, 30);
            tb.Format.HorizontalAlignment = ShapeHorizontalAlignment.Left;
            tb.Format.LineColor = Color.DarkBlue;
            tb.Format.LineStyle = TextBoxLineStyle.Simple;
            tb.Format.FillColor = Color.MediumPurple;
            Paragraph p = tb.Body.AddParagraph();
            TextRange tr = p.AppendText("Textbox in Word Document");
            tr.CharacterFormat.FontName = "Century Gothic";
            tr.CharacterFormat.FontSize = 20;
            tr.CharacterFormat.TextColor = Color.White;
            p.Format.HorizontalAlignment = HorizontalAlignment.Center;
            doc.SaveToFile(outputFileName);
        }

        public static void InsertComment(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            Paragraph p = doc.LastSection.Paragraphs[0];
            Comment comment = p.AppendComment("Produced by Microsoft");
            comment.Format.Author = "Ish'kafel";
            comment.Format.Initial = "Dark Seer";
            doc.SaveToFile(outputFileName);
        }

        public static void InsertFootnote(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            Paragraph p = doc.LastSection.Paragraphs[0];
            Footnote footnote = p.AppendFootnote(FootnoteType.Footnote);
            footnote.TextBody.AddParagraph().AppendText("This is a footnote.");
            doc.SaveToFile(outputFileName);
        }

        public static void InsertEndnote(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            Paragraph p = doc.LastSection.Paragraphs[0];
            Footnote footnote = p.AppendFootnote(FootnoteType.Endnote);
            footnote.TextBody.AddParagraph().AppendText("This is an endnote.");
            doc.SaveToFile(outputFileName);
        }

        public static void FindAndHighlight(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            TextSelection[] selections = doc.FindAllString("Adventure", false, false);
            foreach (TextSelection ts in selections)
            {
                ts.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
            }
            doc.SaveToFile(outputFileName);
        }

    }
}
